//
//  CustomKeyBoard.m
//  GuoBang
//
//  Created by 梧桐树 on 16/4/27.
//  Copyright © 2016年 梧桐树. All rights reserved.
//

#import "CustomKeyBoard.h"

@implementation CustomKeyBoard
-(instancetype)initWithFrame:(CGRect)frame{

    self = [super initWithFrame:frame];
    if (self) {
        _functionIsShow=2;
        _kbIsShow=2;
        _emojiIsShow=2;
        self.lineNum=1;
        //左侧切换键盘按键
        _keyBoardTypeBtn=[[UIButton alloc]initWithFrame:CGRectMake(16, (self.frame.size.height-30)/2, 30, 30)];
        //[keyBoardTypeBtn setBackgroundImage:[UIImage imageNamed:@"ico_add@2x.png"] forState:UIControlStateNormal];
        [_keyBoardTypeBtn setImage:[UIImage imageNamed:@"ico_add@2x.png"] forState:UIControlStateNormal];
        [_keyBoardTypeBtn addTarget:self action:@selector(keyboardTypess) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_keyBoardTypeBtn];
        //右侧功能按键
        functionBtn=[[UIButton alloc]initWithFrame:CGRectMake(WIDTH-46, (self.frame.size.height-30)/2, 30, 30)];
        [functionBtn addTarget:self action:@selector(function) forControlEvents:UIControlEventTouchUpInside];
        [functionBtn setImage:[UIImage imageNamed:@"ico_menu@2x.png"] forState:UIControlStateNormal];
      //  [functionBtn setBackgroundImage:[UIImage imageNamed:@"ico_menu@2x.png"] forState:UIControlStateNormal];
        [self addSubview:functionBtn];
        //聊天框背景
        bgView=[[UIView alloc]initWithFrame:CGRectMake(_keyBoardTypeBtn.frame.size.width+32,(self.frame.size.height-29)/2 , WIDTH-(_keyBoardTypeBtn.frame.size.width+functionBtn.frame.size.width+64),29)];
        bgView.backgroundColor=[SEATools stringToColor:@"efeff4"];
        //聊天框
        textsView=[[UITextView alloc]initWithFrame:CGRectMake(0,0, WIDTH-(_keyBoardTypeBtn.frame.size.width+functionBtn.frame.size.width+64),29)];
        textsView.textColor=[SEATools stringToColor:@"333333"];
        textsView.backgroundColor=[SEATools stringToColor:@"efeff4"];
        textsView.font=[UIFont systemFontOfSize:15];
        textsView.enablesReturnKeyAutomatically = YES;
        textsView.delegate=self;
        textsView.returnKeyType=UIReturnKeySend;
        [bgView addSubview:textsView];
        [self addSubview:bgView];
        _luyin=[[UIButton alloc]initWithFrame:bgView.frame];
        [_luyin setTitle:@"按住说话" forState:UIControlStateNormal];
        [_luyin setBackgroundColor:[SEATools stringToColor:@"EFEFF4"]];
        _luyin.layer.cornerRadius=5;
        [_luyin setTitleColor:[SEATools stringToColor:@"434051"] forState:UIControlStateNormal];
        _luyin.titleLabel.font=[UIFont systemFontOfSize:15];
        [_luyin.layer setBorderWidth:0.8];
        _luyin.layer.borderColor=[SEATools stringToColor:@"c4c4cc"].CGColor;

        [self addSubview:_luyin];
        _luyin.hidden=YES;
     
        placehoderLaber = [[UILabel alloc] initWithFrame:CGRectMake(bgView.frame.origin.x+5, bgView.frame.origin.y, textsView.frame.size.width, bgView.frame.size.height)];
        placehoderLaber.numberOfLines=1;
        placehoderLaber.textColor=[SEATools stringToColor:@"999999"];
        placehoderLaber.textAlignment=NSTextAlignmentLeft;
        placehoderLaber.text = @"我要什么。。。";
        placehoderLaber.font = [UIFont systemFontOfSize:15];
        [self addSubview:placehoderLaber];
        
        //设置view
        self.backgroundColor=[SEATools stringToColor:@"f8f8f8"];
    }
    return self;
}
#pragma mark 根据输入更改高度，结束编辑输入
// 回车就结束编辑
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if ([text isEqualToString:@"\n"]) {
        [self.delegate sendMessage:textsView.text];
        self.lineNum=1;
        textsView.text=@"1";
        [textsView deleteBackward];
        textsView.frame=CGRectMake(textsView.frame.origin.x,textsView.frame.origin.y,textsView.frame.size.width,29);
        bgView.frame=CGRectMake(bgView.frame.origin.x,bgView.frame.origin.y , bgView.frame.size.width,29);
        self.frame=CGRectMake(self.frame.origin.x,HEIGHT-48, self.frame.size.width,48);
        [textView resignFirstResponder];
        return NO;
    }
    return YES;
}
// 当输入时让label消失 根据输入更改高度
-(void)textViewDidChange:(UITextView *)textView{

    CGSize size=[textView sizeThatFits:CGSizeMake(textView.frame.size.width,CGFLOAT_MAX)];
    UIFont *font = [UIFont systemFontOfSize:15];
    NSInteger lines = (NSInteger)(size.height / font.lineHeight);
    
    if (self.lineNum!=(int)lines) {
        self.lineNum=(int)lines;
        if (self.lineNum<5) {
           
            [self changeLine:self.lineNum fontHeight:font.lineHeight];
            
        }
    }

    if(textView.text.length==0){
        
        placehoderLaber.hidden=NO;
        
        
    }else{
        
        placehoderLaber.hidden=YES;
        
    }
}
-(void)changeLine:(int)lineNum fontHeight:(int)fontHeight{
        //NSLog(@"%@");
    textsView.frame=CGRectMake(textsView.frame.origin.x,textsView.frame.origin.y,textsView.frame.size.width,29+fontHeight*(lineNum-1));
    bgView.frame=CGRectMake(bgView.frame.origin.x,bgView.frame.origin.y , bgView.frame.size.width,29+fontHeight*(lineNum-1));
    if (emojiView) {
         emojiView.frame=CGRectMake(0, 48+fontHeight*(lineNum-1), WIDTH, emojiView.frame.size.height);
    }
   
    [self.delegate changeTabFrame:lineNum fontHeight:fontHeight emojiHi:_emojiHi];
   
}
#pragma mark function
-(void)function{
    
    if (_functionIsShow==1) {
        [self functionHiden];
    }else if(_functionIsShow==2){
       
        [self functionShow];
    }
    
   
}
-(void)functionShow{
    [self keyboardHiden];
    [self emojiHiden];
    [self.keyBoardTypeBtn setImage:[UIImage imageNamed:@"ico_add@2x.png"] forState:UIControlStateNormal];
    [textsView resignFirstResponder];
    if (!functionView) {
        functionView=[[FunctionView alloc]initWithFrame:CGRectMake(0, self.frame.size.height, WIDTH, 1)];
        functionView.delegate=self;
        
    }else{
    
        functionView.frame=CGRectMake(0, self.frame.size.height, WIDTH, functionView.frame.size.height);
    }

    [self addSubview:functionView];
    self.frame=CGRectMake(self.frame.origin.x, self.frame.origin.y-functionView.frame.size.height, WIDTH, self.frame.size.height+functionView.frame.size.height);
    [self.delegate showFunction:_functionIsShow height:functionView.frame.size.height];
    _functionIsShow=1;
}
-(void)functionHiden{
    if (_functionIsShow==2) {
        return;
    }
    [functionView removeFromSuperview];
    self.frame=CGRectMake(self.frame.origin.x, self.frame.origin.y+functionView.frame.size.height, WIDTH, self.frame.size.height-functionView.frame.size.height);
    [self.delegate showFunction:_functionIsShow height:functionView.frame.size.height];
        _functionIsShow=2;
}
#pragma mark FunctionViewDelegate
-(void)selectFunction:(int)tag{

    switch (tag) {
            //提醒
        case 0:
        {
            [self.delegate tixingAction];
        }
            break;
            //话费
        case 1:
        {
            [self.delegate sendMessage:@"我要充值话费！"];
        }
            break;
            //咖啡
        case 2:
        {
            [self.delegate sendMessage:@"我要喝咖啡！"];

        }
            break;
            //打车
        case 3:
        {
            [self.delegate sendMessage:@"我要打车！"];

        }
            break;
            //下午茶
        case 4:
        {
            [self.delegate sendMessage:@"我要点下午茶！"];

        }
            break;
            //零食
        case 5:
        {
            [self.delegate sendMessage:@"我要吃零食！"];

        }
            break;
            //水果
        case 6:
        {
            [self.delegate sendMessage:@"我要吃水果！"];

        }
            break;
            //更多服务
        case 7:
        {
            [self.delegate sendMessage:@"有没有更多服务？"];

        }
            break;
            
        default:
            break;
    }
}
#pragma mark keyBoard
-(void)keyboardTypess{
    
       if (_kbIsShow==1) {
           if (textsView.text.length==0) {
               placehoderLaber.hidden=NO;
               
           }
        [textsView becomeFirstResponder];
       
    }else if(_kbIsShow==2){
        [textsView resignFirstResponder];
        [self keyboardShow];
        [self.keyBoardTypeBtn setImage:[UIImage imageNamed:@"Keyboard-64@2x.png"] forState:UIControlStateNormal];
    
    }

    
}
-(void)keyboardShow{
    [self functionHiden];
    [self emojiHiden];
    [textsView resignFirstResponder];
    if (!keyboardView) {
        keyboardView=[[KeyBoardTypeView alloc]initWithFrame:CGRectMake(0, self.frame.size.height, WIDTH, 1)];
        keyboardView.delegate=self;
        
    }else{
        
        keyboardView.frame=CGRectMake(0, self.frame.size.height, WIDTH, keyboardView.frame.size.height);
    }
    
    [self addSubview:keyboardView];
    self.frame=CGRectMake(self.frame.origin.x, self.frame.origin.y-keyboardView.frame.size.height, WIDTH, self.frame.size.height+keyboardView.frame.size.height);
    [self.delegate showFunction:_kbIsShow height:keyboardView.frame.size.height];
    _kbIsShow=1;
}
-(void)keyboardHiden{
    if (_kbIsShow==2) {
        return;
    }
    [keyboardView removeFromSuperview];
    
    self.frame=CGRectMake(self.frame.origin.x, self.frame.origin.y+keyboardView.frame.size.height, WIDTH, self.frame.size.height-keyboardView.frame.size.height);
    [self.delegate showFunction:_kbIsShow height:keyboardView.frame.size.height];
    _kbIsShow=2;

}
#pragma mark customKeyboardDelegate
-(void)touchKey:(int)row{
    switch (row) {
            //语音
        case 0:
        {
            self.lineNum=1;
            textsView.text=@"1";
            [textsView deleteBackward];
            [self keyboardHiden];
            textsView.frame=CGRectMake(textsView.frame.origin.x,textsView.frame.origin.y,textsView.frame.size.width,29);
            bgView.frame=CGRectMake(bgView.frame.origin.x,bgView.frame.origin.y , bgView.frame.size.width,29);
            self.frame=CGRectMake(self.frame.origin.x,HEIGHT-48, self.frame.size.width,48);
            
            [self.keyBoardTypeBtn setImage:[UIImage imageNamed:@"ico_add@2x.png"] forState:UIControlStateNormal];
            _luyin.hidden=NO;
            placehoderLaber.hidden=YES;
            [self.luyin addTarget:self action:@selector(myYuYin:) forControlEvents:UIControlEventTouchDown];
            [self.luyin addTarget:self action:@selector(stopYuYin:) forControlEvents:UIControlEventTouchUpInside];
            [self.luyin addTarget:self action:@selector(noYuyin:) forControlEvents:UIControlEventTouchDragExit];
            NSLog(@"切换至语音");
            
        }
            break;
            //EMOJI
        case 1:
        {
            if (textsView.text.length==0) {
                placehoderLaber.hidden=NO;

            }
            _luyin.hidden=YES;
            [self emoji];
            
        }
            break;
            //图片
        case 2:
        {
            [self.delegate selectPhoto];
        }
            break;
            //拍照
        case 3:
        {
            [self.delegate takePhoto];
        }
            break;
            
        default:
            break;
    }


}
#pragma mark EMOJI
-(void)emoji{
    
    if (_emojiIsShow==1) {
        [self emojiHiden];
    }else if(_emojiIsShow==2){
        
        [self emojiShow];
    }
    
    
}
-(void)emojiShow{
    [self keyboardHiden];
    [self functionHiden];
    [self.keyBoardTypeBtn setImage:[UIImage imageNamed:@"ico_add@2x.png"] forState:UIControlStateNormal];
    [textsView resignFirstResponder];
    if (!emojiView) {
        emojiView=[[EmojiView alloc]initWithFrame:CGRectMake(0, self.frame.size.height, WIDTH, 1)];
        emojiView.delegate=self;
        
    }else{
        
        emojiView.frame=CGRectMake(0, self.frame.size.height, WIDTH, emojiView.frame.size.height);
    }
    _emojiHi=emojiView.frame.size.height;
    [self addSubview:emojiView];
    self.frame=CGRectMake(self.frame.origin.x, self.frame.origin.y-emojiView.frame.size.height, WIDTH, self.frame.size.height+emojiView.frame.size.height);
    [self.delegate showFunction:_functionIsShow height:emojiView.frame.size.height];
    _emojiIsShow=1;
}
-(void)emojiHiden{
    _emojiHi=0;
    if (_emojiIsShow==2) {
        return;
    }
    [emojiView removeFromSuperview];
    self.frame=CGRectMake(self.frame.origin.x, self.frame.origin.y+emojiView.frame.size.height, WIDTH, self.frame.size.height-emojiView.frame.size.height);
    [self.delegate showFunction:_functionIsShow height:emojiView.frame.size.height];
    _emojiIsShow=2;
}
#pragma mark emojiDelegate
-(void)addEmoji:(NSString *)emoji{
    placehoderLaber.hidden=YES;

    [textsView insertText:emoji];
    [textsView scrollRectToVisible:CGRectMake(0, textsView.contentSize.height-15, textsView.contentSize.width, 10) animated:YES];
}
-(void)dele{

    [textsView deleteBackward];
}
-(void)send{
    
    if (textsView.text.length!=0) {
        [self.delegate sendMessage:textsView.text];
        self.lineNum=1;
        textsView.text=@"1";
        [textsView deleteBackward];
        textsView.frame=CGRectMake(textsView.frame.origin.x,textsView.frame.origin.y,textsView.frame.size.width,29);
        bgView.frame=CGRectMake(bgView.frame.origin.x,bgView.frame.origin.y , bgView.frame.size.width,29);
        [self.delegate emojiHidens];
        _emojiHi=0;
        [emojiView removeFromSuperview];
        //self.frame=CGRectMake(self.frame.origin.x,HEIGHT-48, self.frame.size.width,48);

        
    }

}


- (void)myYuYin:(UIButton *)sender
{
    
    
    NSLog(@"1231231231244444465555");
    
    [self.delegate yuyinAction];
    
}


- (void)stopYuYin:(UIButton *)sender
{
    
    [self.delegate yuyinStop];
    
    
}
-(void)noYuyin:(UIButton *)sender{
    [self.delegate yuyinNo];
}



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
